

package com.hazelcast.config.security;

import com.hazelcast.config.LoginModuleConfig;
import com.hazelcast.config.LoginModuleConfig.LoginModuleUsage;

import java.util.Objects;
import java.util.Properties;

/**
 * Typed authentication configuration for the {@code X509CertificateLoginModule}.
 */
public class TlsAuthenticationConfig extends AbstractClusterLoginConfig<TlsAuthenticationConfig> {

    private String roleAttribute;

    public String getRoleAttribute() {
        return roleAttribute;
    }

    public TlsAuthenticationConfig setRoleAttribute(String roleAttribute) {
        this.roleAttribute = roleAttribute;
        return this;
    }

    @Override
    protected Properties initLoginModuleProperties() {
        Properties props = super.initLoginModuleProperties();
        setIfConfigured(props, "roleAttribute", roleAttribute);
        return props;
    }

    @Override
    public LoginModuleConfig[] asLoginModuleConfigs() {
        LoginModuleConfig loginModuleConfig = new LoginModuleConfig("com.hazelcast.security.loginimpl.X509CertificateLoginModule", LoginModuleUsage.REQUIRED);
        loginModuleConfig.setProperties(initLoginModuleProperties());
        return new LoginModuleConfig[]{loginModuleConfig};
    }

    @Override
    public String toString() {
        return "TlsAuthenticationConfig [roleAttribute=" + roleAttribute + ", getSkipIdentity()=" + getSkipIdentity() + ", getSkipEndpoint()=" + getSkipEndpoint() + ", getSkipRole()=" + getSkipRole() + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + Objects.hash(roleAttribute);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        TlsAuthenticationConfig other = (TlsAuthenticationConfig) obj;
        return Objects.equals(roleAttribute, other.roleAttribute);
    }

    @Override
    protected TlsAuthenticationConfig self() {
        return this;
    }
}
